package utils.wallpaperHandler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import utils.fileHandler.FileHandler;
import utils.jsonHandler.JsonReadWrite;
import utils.utilTime.UtilTime;

import java.io.File;

import static utils.jsonHandler.JsonReadWrite.writeJsonFile;

public class WallpaperHandler {
    private static JSONObject wallpaperObject;
    public static final int preImageType = 1;
    public static final int hDImageType = 2;
    public static final int preVideoType = 3;
    public static final int hDVideoType = 4;
    private static JSONObject preTohDImage;
    private static JSONObject hDToPreImage;
    private static JSONObject preTohDVideo;
    private static JSONObject hDToPreVideo;

    static {
        wallpaperObject = JSON.parseObject(JsonReadWrite.readJsonFile(FileHandler.getWallpaperJSON()));
        preTohDImage = wallpaperObject.getJSONObject("preTohDImage");
        hDToPreImage = wallpaperObject.getJSONObject("hDToPreImage");
        preTohDVideo = wallpaperObject.getJSONObject("preTohDVideo");
        hDToPreVideo = wallpaperObject.getJSONObject("hDToPreVideo");
    }

    public static void save() {
        wallpaperObject.put("preTohDImage", preTohDImage);
        wallpaperObject.put("hDToPreImage", hDToPreImage);
        wallpaperObject.put("preTohDVideo", preTohDVideo);
        wallpaperObject.put("hDToPreVideo", hDToPreVideo);
        writeJsonFile(FileHandler.getWallpaperJSON(), wallpaperObject.toJSONString());
        System.out.println("Log : save " + FileHandler.getWallpaperJSON() + " success" + UtilTime.getStringTimeNow());
    }

    public static void exit() {
        save();
    }

    public static void addWallpaper(String prePath, String hDPath, int type) {
        System.out.println(prePath);
        System.out.println(hDPath);
        if (type == preImageType || type == hDImageType) {
            preTohDImage.put(prePath.replace(FileHandler.getPreImageDirAbsolutePath()+ File.separator,""), hDPath.replace(FileHandler.gethDImageDirAbsolutePath()+ File.separator,""));
            hDToPreImage.put(hDPath.replace(FileHandler.gethDImageDirAbsolutePath()+ File.separator,""), prePath.replace(FileHandler.getPreImageDirAbsolutePath()+ File.separator,""));
        } else if (type == hDVideoType || type == preVideoType) {
            preTohDVideo.put(prePath.replace(FileHandler.getPreVideoDirAbsolutePath()+ File.separator,""), hDPath.replace(FileHandler.gethDVideoDirAbsolutePath()+ File.separator,""));
            hDToPreVideo.put(hDPath.replace(FileHandler.gethDVideoDirAbsolutePath()+ File.separator,""), prePath.replace(FileHandler.getPreVideoDirAbsolutePath()+ File.separator,""));
        }
    }

    public static void delete(String path, int type) {
        switch (type) {
            case preImageType:
                path=path.replace(FileHandler.getPreImageDirAbsolutePath()+ File.separator,"");
                hDToPreImage.remove(preTohDImage.getString(path));
                preTohDImage.remove(path);
                break;
            case hDImageType:
                path=path.replace(FileHandler.gethDImageDirAbsolutePath()+ File.separator,"");
                preTohDImage.remove(hDToPreImage.getString(path));
                hDToPreImage.remove(path);
                break;
            case preVideoType:
                path=path.replace(FileHandler.getPreVideoDirAbsolutePath()+ File.separator,"");
                hDToPreVideo.remove(preTohDVideo.getString(path));
                preTohDVideo.remove(path);
                break;
            case hDVideoType:
                path=path.replace(FileHandler.gethDVideoDirAbsolutePath()+ File.separator,"");
                preTohDVideo.remove(hDToPreVideo.getString(path));
                hDToPreVideo.remove(path);
                break;
        }
    }

    public static String getPreImagePath(String hDImagePath) {
        return FileHandler.getPreImageDirAbsolutePath()+ File.separator + hDToPreImage.getString(hDImagePath.replace(FileHandler.gethDImageDirAbsolutePath()+ File.separator,""));
    }

    public static String gethDImagePath(String preImagePath) {
        return FileHandler.gethDImageDirAbsolutePath()+ File.separator+ preTohDImage.getString(preImagePath.replace(FileHandler.getPreImageDirAbsolutePath()+ File.separator,""));
    }

    public static String getPreVideoPath(String hDVideoPath) {
        return FileHandler.getPreVideoDirAbsolutePath()+ File.separator+ hDToPreVideo.getString(hDVideoPath.replace(FileHandler.gethDVideoDirAbsolutePath()+ File.separator,""));
    }

    public static String gethDVideoPath(String preVideoPath) {
        return FileHandler.gethDVideoDirAbsolutePath()+ File.separator+ preTohDVideo.getString(preVideoPath.replace(FileHandler.getPreVideoDirAbsolutePath()+ File.separator,""));
    }

}
